//###############
//FILE : WantFileRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.nameserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameWantFileSession;

/**
 * This handles the WANTFILE request as specified in the assignment.
 * @author daniel
 */
public class WantFileRequest extends FileManagerSpecificRequest{
    private NameWantFileSession _wantFileSession;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameWantFileSession.
     * @param nsDb The nameserver database.
     */
    public WantFileRequest(NameServerDatabase nsDb) {
        super(nsDb, new NameWantFileSession());
        _wantFileSession = (NameWantFileSession)_openingSession;
    }

    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving want all files request");

        //first run the want all files session
        _wantFileSession.setNameServerDatabase(_nameServerDb);

        if (!_wantFileSession.executeSession(inputStream, outputStream)) {
            return false;
        }


        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }
        
        return true;

    }

    @Override
    public NameServerRequest getCopy() {
        WantFileRequest copy = new WantFileRequest(_nameServerDb);
        return copy;
    }

}
